#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
#![feature(extern_types)]
extern crate libc;
use primary::alloc_testing::*;
use primary::list::*;
use primary::compare_pointer::*;
use primary::compare_string::*;
use primary::compare_int::*;
use primary::hash_pointer::*;
use primary::hash_string::*;
use primary::hash_int::*;
extern "C" {
    pub type _ListEntry;
    fn __assert_fail(
        __assertion: *const libc::c_char,
        __file: *const libc::c_char,
        __line: libc::c_uint,
        __function: *const libc::c_char,
    ) -> !;
                                                                                        }
#[no_mangle]
pub static mut variable1: libc::c_int = 50 as libc::c_int;
#[no_mangle]
pub static mut variable2: libc::c_int = 0;
#[no_mangle]
pub static mut variable3: libc::c_int = 0;
#[no_mangle]
pub static mut variable4: libc::c_int = 0;
#[no_mangle]
pub extern "C" fn generate_list() -> *mut ListEntry {
    let mut list: *mut ListEntry = std::ptr::null_mut();

    assert!(
        !list_append(&mut list, unsafe { &mut variable1 as *mut libc::c_int as ListValue }).is_null(),
        "list_append(&list, &variable1) != NULL"
    );
    assert!(
        !list_append(&mut list, unsafe { &mut variable2 as *mut libc::c_int as ListValue }).is_null(),
        "list_append(&list, &variable2) != NULL"
    );
    assert!(
        !list_append(&mut list, unsafe { &mut variable3 as *mut libc::c_int as ListValue }).is_null(),
        "list_append(&list, &variable3) != NULL"
    );
    assert!(
        !list_append(&mut list, unsafe { &mut variable4 as *mut libc::c_int as ListValue }).is_null(),
        "list_append(&list, &variable4) != NULL"
    );

    list
}
#[no_mangle]
pub extern "C" fn check_list_integrity(mut list: *mut ListEntry) {
    let mut prev: *mut ListEntry = std::ptr::null_mut();
    let mut rover: *mut ListEntry = list;

    while !rover.is_null() {
        
        unsafe {
            assert!(list_prev(rover) == prev, "list_prev(rover) == prev");
        }
        prev = rover;
        
        rover = unsafe { list_next(rover) };
    }
}
#[no_mangle]
pub extern "C" fn test_list_append() {
    let mut list: *mut ListEntry = std::ptr::null_mut();

    assert!(!list_append(&mut list, unsafe { &mut variable1 as *mut libc::c_int as ListValue }).is_null());
    check_list_integrity(list);
    assert!(!list_append(&mut list, unsafe { &mut variable2 as *mut libc::c_int as ListValue }).is_null());
    check_list_integrity(list);
    assert!(!list_append(&mut list, unsafe { &mut variable3 as *mut libc::c_int as ListValue }).is_null());
    check_list_integrity(list);
    assert!(!list_append(&mut list, unsafe { &mut variable4 as *mut libc::c_int as ListValue }).is_null());
    check_list_integrity(list);
    assert_eq!(list_length(list), 4);
    assert_eq!(list_nth_data(list, 0), unsafe { &mut variable1 as *mut libc::c_int as ListValue });
    assert_eq!(list_nth_data(list, 1), unsafe { &mut variable2 as *mut libc::c_int as ListValue });
    assert_eq!(list_nth_data(list, 2), unsafe { &mut variable3 as *mut libc::c_int as ListValue });
    assert_eq!(list_nth_data(list, 3), unsafe { &mut variable4 as *mut libc::c_int as ListValue });
    list_free(list);
}
#[no_mangle]
pub extern "C" fn test_list_prepend() {
    let mut list: *mut ListEntry = std::ptr::null_mut();

    assert!(
        !list_prepend(&mut list, unsafe { &mut variable1 as *mut libc::c_int as ListValue }).is_null(),
        "list_prepend(&list, &variable1) != NULL"
    );
    check_list_integrity(list);
    assert!(
        !list_prepend(&mut list, unsafe { &mut variable2 as *mut libc::c_int as ListValue }).is_null(),
        "list_prepend(&list, &variable2) != NULL"
    );
    check_list_integrity(list);
    assert!(
        !list_prepend(&mut list, unsafe { &mut variable3 as *mut libc::c_int as ListValue }).is_null(),
        "list_prepend(&list, &variable3) != NULL"
    );
    check_list_integrity(list);
    assert!(
        !list_prepend(&mut list, unsafe { &mut variable4 as *mut libc::c_int as ListValue }).is_null(),
        "list_prepend(&list, &variable4) != NULL"
    );
    check_list_integrity(list);
    assert_eq!(
        list_nth_data(list, 0),
        unsafe { &mut variable4 as *mut libc::c_int as ListValue },
        "list_nth_data(list, 0) == &variable4"
    );
    assert_eq!(
        list_nth_data(list, 1),
        unsafe { &mut variable3 as *mut libc::c_int as ListValue },
        "list_nth_data(list, 1) == &variable3"
    );
    assert_eq!(
        list_nth_data(list, 2),
        unsafe { &mut variable2 as *mut libc::c_int as ListValue },
        "list_nth_data(list, 2) == &variable2"
    );
    assert_eq!(
        list_nth_data(list, 3),
        unsafe { &mut variable1 as *mut libc::c_int as ListValue },
        "list_nth_data(list, 3) == &variable1"
    );
    list_free(list);
}
#[no_mangle]
pub extern "C" fn test_list_free() {
    let mut list: *mut ListEntry = std::ptr::null_mut();

    list = unsafe { generate_list() };

    let list_box = unsafe { Box::from_raw(list) };
    list_free(list);
    list = Box::into_raw(list_box);

    list_free(std::ptr::null_mut());
}
#[no_mangle]
pub extern "C" fn test_list_next() {
    let mut list: *mut ListEntry = std::ptr::null_mut();
    let mut rover: *mut ListEntry = std::ptr::null_mut();

    
    unsafe {
        list = generate_list();
    }

    rover = list;

    
    unsafe {
        assert_eq!(list_data(rover), &mut variable1 as *mut libc::c_int as ListValue, "list_data(rover) == &variable1");
        rover = list_next(rover);
        assert_eq!(list_data(rover), &mut variable2 as *mut libc::c_int as ListValue, "list_data(rover) == &variable2");
        rover = list_next(rover);
        assert_eq!(list_data(rover), &mut variable3 as *mut libc::c_int as ListValue, "list_data(rover) == &variable3");
        rover = list_next(rover);
        assert_eq!(list_data(rover), &mut variable4 as *mut libc::c_int as ListValue, "list_data(rover) == &variable4");
        rover = list_next(rover);
        assert!(rover.is_null(), "rover == NULL");
    }

    
    unsafe {
        list_free(list);
    }
}
#[no_mangle]
pub extern "C" fn test_list_nth_entry() {
    let mut list: *mut ListEntry = std::ptr::null_mut();
    let mut entry: *mut ListEntry = std::ptr::null_mut();

    list = generate_list();
    let list_box = unsafe { Box::from_raw(list) };

    entry = list_nth_entry(list, 0);
    let entry_box_0 = unsafe { Box::from_raw(entry) };
    assert_eq!(
        list_data(entry),
        unsafe { &mut variable1 as *mut libc::c_int as ListValue },
        "list_data(entry) == &variable1"
    );
    entry = Box::into_raw(entry_box_0);

    entry = list_nth_entry(list, 1);
    let entry_box_1 = unsafe { Box::from_raw(entry) };
    assert_eq!(
        list_data(entry),
        unsafe { &mut variable2 as *mut libc::c_int as ListValue },
        "list_data(entry) == &variable2"
    );
    entry = Box::into_raw(entry_box_1);

    entry = list_nth_entry(list, 2);
    let entry_box_2 = unsafe { Box::from_raw(entry) };
    assert_eq!(
        list_data(entry),
        unsafe { &mut variable3 as *mut libc::c_int as ListValue },
        "list_data(entry) == &variable3"
    );
    entry = Box::into_raw(entry_box_2);

    entry = list_nth_entry(list, 3);
    let entry_box_3 = unsafe { Box::from_raw(entry) };
    assert_eq!(
        list_data(entry),
        unsafe { &mut variable4 as *mut libc::c_int as ListValue },
        "list_data(entry) == &variable4"
    );
    entry = Box::into_raw(entry_box_3);

    entry = list_nth_entry(list, 4);
    assert!(entry.is_null(), "entry == NULL");

    entry = list_nth_entry(list, 400);
    assert!(entry.is_null(), "entry == NULL");

    list = Box::into_raw(list_box);
    list_free(list);
}
#[no_mangle]
pub extern "C" fn test_list_nth_data() {
    let mut list: *mut ListEntry = std::ptr::null_mut();
    unsafe {
        list = generate_list();
    }
    let list_box = unsafe { Box::from_raw(list) };
    assert_eq!(
        list_nth_data(list, 0),
        unsafe { &mut variable1 as *mut libc::c_int as ListValue },
        "list_nth_data(list, 0) == &variable1"
    );
    assert_eq!(
        list_nth_data(list, 1),
        unsafe { &mut variable2 as *mut libc::c_int as ListValue },
        "list_nth_data(list, 1) == &variable2"
    );
    assert_eq!(
        list_nth_data(list, 2),
        unsafe { &mut variable3 as *mut libc::c_int as ListValue },
        "list_nth_data(list, 2) == &variable3"
    );
    assert_eq!(
        list_nth_data(list, 3),
        unsafe { &mut variable4 as *mut libc::c_int as ListValue },
        "list_nth_data(list, 3) == &variable4"
    );
    assert!(
        list_nth_data(list, 4).is_null(),
        "list_nth_data(list, 4) == NULL"
    );
    assert!(
        list_nth_data(list, 400).is_null(),
        "list_nth_data(list, 400) == NULL"
    );
    list = Box::into_raw(list_box);
    unsafe {
        list_free(list);
    }
}
#[no_mangle]
pub extern "C" fn test_list_length() {
    let mut list: *mut ListEntry = std::ptr::null_mut();

    list = generate_list();
    let list_box = unsafe { Box::from_raw(list) };
    assert_eq!(list_length(list), 4);
    assert!(!list_prepend(&mut list, unsafe { &mut variable1 as *mut libc::c_int as ListValue }).is_null());
    assert_eq!(list_length(list), 5);
    list = Box::into_raw(list_box);
    list_free(list);
    assert_eq!(list_length(std::ptr::null_mut()), 0);
}
#[no_mangle]
pub extern "C" fn test_list_remove_entry() {
    let mut empty_list: *mut ListEntry = std::ptr::null_mut();
    let mut list: *mut ListEntry = std::ptr::null_mut();
    let mut entry: *mut ListEntry = std::ptr::null_mut();

    unsafe {
        list = generate_list();
        entry = list_nth_entry(list, 2);
    }

    assert_ne!(unsafe { list_remove_entry(&mut list, entry) }, 0, "list_remove_entry(&list, entry) != 0");
    assert_eq!(unsafe { list_length(list) }, 3, "list_length(list) == 3");
    check_list_integrity(list);

    unsafe {
        entry = list_nth_entry(list, 0);
    }

    assert_ne!(unsafe { list_remove_entry(&mut list, entry) }, 0, "list_remove_entry(&list, entry) != 0");
    assert_eq!(unsafe { list_length(list) }, 2, "list_length(list) == 2");
    check_list_integrity(list);

    assert_eq!(unsafe { list_remove_entry(&mut list, std::ptr::null_mut()) }, 0, "list_remove_entry(&list, NULL) == 0");
    assert_eq!(unsafe { list_remove_entry(&mut empty_list, std::ptr::null_mut()) }, 0, "list_remove_entry(&empty_list, NULL) == 0");

    unsafe {
        list_free(list);
    }
    list = std::ptr::null_mut();

    assert!(!unsafe { list_append(&mut list, &mut variable1 as *mut libc::c_int as ListValue) }.is_null(), "list_append(&list, &variable1) != NULL");
    assert!(!list.is_null(), "list != NULL");
    assert_ne!(unsafe { list_remove_entry(&mut list, list) }, 0, "list_remove_entry(&list, list) != 0");
    assert!(list.is_null(), "list == NULL");

    unsafe {
        list = generate_list();
        entry = list_nth_entry(list, 3);
    }

    assert_ne!(unsafe { list_remove_entry(&mut list, entry) }, 0, "list_remove_entry(&list, entry) != 0");
    check_list_integrity(list);
    unsafe {
        list_free(list);
    }
}
#[no_mangle]
pub extern "C" fn test_list_remove_data() {
    let mut entries: [libc::c_int; 13] = [
        89, 4, 23, 42, 4, 16, 15, 4, 8, 99, 50, 30, 4,
    ];
    let mut num_entries: libc::c_uint = (std::mem::size_of::<[libc::c_int; 13]>()
        as libc::c_ulong)
        .wrapping_div(std::mem::size_of::<libc::c_int>() as libc::c_ulong)
        as libc::c_uint;
    let mut val: libc::c_int = 0;
    let mut list: *mut ListEntry = std::ptr::null_mut();
    let mut i: libc::c_uint = 0;

    while i < num_entries {
        unsafe {
            assert!(
                !list_prepend(
                    &mut list,
                    &mut *entries.as_mut_ptr().offset(i as isize) as *mut libc::c_int
                        as ListValue,
                )
                .is_null(),
                "list_prepend(&list, &entries[i]) != NULL"
            );
        }
        i = i.wrapping_add(1);
    }

    val = 0;
    assert_eq!(
        list_remove_data(
            &mut list,
            Some(int_equal),
            &mut val as *mut libc::c_int as ListValue,
        ),
        0,
        "list_remove_data(&list, int_equal, &val) == 0"
    );

    val = 56;
    assert_eq!(
        list_remove_data(
            &mut list,
            Some(int_equal),
            &mut val as *mut libc::c_int as ListValue,
        ),
        0,
        "list_remove_data(&list, int_equal, &val) == 0"
    );

    unsafe { check_list_integrity(list); }

    val = 8;
    assert_eq!(
        list_remove_data(
            &mut list,
            Some(int_equal),
            &mut val as *mut libc::c_int as ListValue,
        ),
        1,
        "list_remove_data(&list, int_equal, &val) == 1"
    );

    unsafe {
        assert_eq!(
            list_length(list),
            num_entries.wrapping_sub(1),
            "list_length(list) == num_entries - 1"
        );
    }

    unsafe { check_list_integrity(list); }

    val = 4;
    assert_eq!(
        list_remove_data(
            &mut list,
            Some(int_equal),
            &mut val as *mut libc::c_int as ListValue,
        ),
        4,
        "list_remove_data(&list, int_equal, &val) == 4"
    );

    unsafe {
        assert_eq!(
            list_length(list),
            num_entries.wrapping_sub(5),
            "list_length(list) == num_entries - 5"
        );
    }

    unsafe { check_list_integrity(list); }

    val = 89;
    assert_eq!(
        list_remove_data(
            &mut list,
            Some(int_equal),
            &mut val as *mut libc::c_int as ListValue,
        ),
        1,
        "list_remove_data(&list, int_equal, &val) == 1"
    );

    unsafe {
        assert_eq!(
            list_length(list),
            num_entries.wrapping_sub(6),
            "list_length(list) == num_entries - 6"
        );
    }

    unsafe { check_list_integrity(list); }

    unsafe { list_free(list); }
}
#[no_mangle]
pub extern "C" fn test_list_sort() {
    let mut list: *mut ListEntry = std::ptr::null_mut();
    let mut entries: [libc::c_int; 13] = [
        89, 4, 23, 42, 4, 16, 15, 4, 8, 99, 50, 30, 4,
    ];
    let mut sorted: [libc::c_int; 13] = [
        4, 4, 4, 4, 8, 15, 16, 23, 30, 42, 50, 89, 99,
    ];
    let num_entries: libc::c_uint = (std::mem::size_of::<[libc::c_int; 13]>() as libc::c_ulong)
        .wrapping_div(std::mem::size_of::<libc::c_int>() as libc::c_ulong)
        as libc::c_uint;
    let mut i: libc::c_uint = 0;

    i = 0;
    while i < num_entries {
        assert!(
            !list_prepend(
                &mut list,
                unsafe { &mut *entries.as_mut_ptr().offset(i as isize) as *mut libc::c_int as ListValue },
            )
            .is_null(),
            "list_prepend(&list, &entries[i]) != NULL"
        );
        i = i.wrapping_add(1);
    }

    list_sort(
        &mut list,
        Some(int_compare),
    );

    assert_eq!(list_length(list), num_entries, "list_length(list) == num_entries");

    i = 0;
    while i < num_entries {
        let mut value: *mut libc::c_int = std::ptr::null_mut();
        value = list_nth_data(list, i) as *mut libc::c_int;
        assert_eq!(unsafe { *value }, sorted[i as usize], "*value == sorted[i]");
        i = i.wrapping_add(1);
    }

    list_free(list);

    list = std::ptr::null_mut();

    list_sort(
        &mut list,
        Some(int_compare),
    );

    assert!(list.is_null(), "list == NULL");
}
#[no_mangle]
pub extern "C" fn test_list_find_data() {
    let mut entries: [libc::c_int; 10] = [
        89, 23, 42, 16, 15, 4, 8, 99, 50, 30,
    ];
    let num_entries: libc::c_int = (std::mem::size_of::<[libc::c_int; 10]>() as libc::c_ulong)
        .wrapping_div(std::mem::size_of::<libc::c_int>() as libc::c_ulong) as libc::c_int;
    let mut list: *mut ListEntry = std::ptr::null_mut();
    let mut result: *mut ListEntry = std::ptr::null_mut();
    let mut i: libc::c_int = 0;
    let mut val: libc::c_int = 0;
    let mut data: *mut libc::c_int = std::ptr::null_mut();

    while i < num_entries {
        unsafe {
            assert!(!list_append(
                &mut list,
                &mut *entries.as_mut_ptr().offset(i as isize) as *mut libc::c_int as ListValue,
            ).is_null(), "list_append(&list, &entries[i]) != NULL");
        }
        i += 1;
    }

    i = 0;
    while i < num_entries {
        val = entries[i as usize];
        result = list_find_data(
            list,
            Some(int_equal),
            &mut val as *mut libc::c_int as ListValue,
        );
        assert!(!result.is_null(), "result != NULL");
        unsafe {
            data = list_data(result) as *mut libc::c_int;
            assert!(*data == val, "*data == val");
        }
        i += 1;
    }

    val = 0;
    assert!(list_find_data(
        list,
        Some(int_equal),
        &mut val as *mut libc::c_int as ListValue,
    ).is_null(), "list_find_data(list, int_equal, &val) == NULL");

    val = 56;
    assert!(list_find_data(
        list,
        Some(int_equal),
        &mut val as *mut libc::c_int as ListValue,
    ).is_null(), "list_find_data(list, int_equal, &val) == NULL");

    unsafe {
        list_free(list);
    }
}
#[no_mangle]
pub extern "C" fn test_list_to_array() {
    let mut list: *mut ListEntry = std::ptr::null_mut();
    let mut array: *mut *mut libc::c_void = std::ptr::null_mut();

    
    unsafe {
        list = generate_list();
    }

    
    unsafe {
        array = list_to_array(list);
    }

    
    unsafe {
        assert!(
            *array.offset(0) == &mut variable1 as *mut libc::c_int as *mut libc::c_void,
            "array[0] == &variable1"
        );
        assert!(
            *array.offset(1) == &mut variable2 as *mut libc::c_int as *mut libc::c_void,
            "array[1] == &variable2"
        );
        assert!(
            *array.offset(2) == &mut variable3 as *mut libc::c_int as *mut libc::c_void,
            "array[2] == &variable3"
        );
        assert!(
            *array.offset(3) == &mut variable4 as *mut libc::c_int as *mut libc::c_void,
            "array[3] == &variable4"
        );
    }

    
    unsafe {
        alloc_test_free(array as *mut libc::c_void);
    }

    
    unsafe {
        list_free(list);
    }
}
#[no_mangle]
pub extern "C" fn test_list_iterate() {
    let mut list: *mut ListEntry = std::ptr::null_mut();
    let mut iter: ListIterator = ListIterator {
        prev_next: std::ptr::null_mut(),
        current: std::ptr::null_mut(),
    };
    let mut i: libc::c_int = 0;
    let mut a: libc::c_int = 0;
    let mut counter: libc::c_int = 0;
    let mut data: *mut libc::c_int = std::ptr::null_mut();

    list = std::ptr::null_mut();
    i = 0;
    while i < 50 {
        assert!(!list_prepend(&mut list, &mut a as *mut libc::c_int as ListValue).is_null(), "list_prepend(&list, &a) != NULL");
        i += 1;
    }

    counter = 0;
    list_iterate(&mut list, &mut iter);
    list_iter_remove(&mut iter);

    while list_iter_has_more(&mut iter) != 0 {
        data = list_iter_next(&mut iter) as *mut libc::c_int;
        counter += 1;
        if counter % 2 == 0 {
            list_iter_remove(&mut iter);
            list_iter_remove(&mut iter);
        }
    }

    assert!(list_iter_next(&mut iter).is_null(), "list_iter_next(&iter) == NULL");
    list_iter_remove(&mut iter);

    assert_eq!(counter, 50, "counter == 50");
    assert_eq!(list_length(list), 25, "list_length(list) == 25");
    list_free(list);

    list = std::ptr::null_mut();
    counter = 0;
    list_iterate(&mut list, &mut iter);

    while list_iter_has_more(&mut iter) != 0 {
        data = list_iter_next(&mut iter) as *mut libc::c_int;
        counter += 1;
    }

    assert_eq!(counter, 0, "counter == 0");
}
#[no_mangle]
pub extern "C" fn test_list_iterate_bad_remove() {
    let mut list: *mut ListEntry = std::ptr::null_mut();
    let mut iter: ListIterator = ListIterator {
        prev_next: std::ptr::null_mut(),
        current: std::ptr::null_mut(),
    };
    let mut values: [libc::c_int; 49] = [0; 49];
    let mut i: libc::c_int = 0;
    let mut val: *mut libc::c_int = std::ptr::null_mut();

    list = std::ptr::null_mut();
    i = 0;
    while i < 49 {
        values[i as usize] = i;
        assert!(
            !list_prepend(
                &mut list,
                unsafe { &mut *values.as_mut_ptr().offset(i as isize) as *mut libc::c_int as ListValue },
            )
            .is_null(),
            "list_prepend(&list, &values[i]) != NULL"
        );
        i += 1;
    }

    list_iterate(&mut list, &mut iter);

    while list_iter_has_more(&mut iter) != 0 {
        val = list_iter_next(&mut iter) as *mut libc::c_int;
        if unsafe { *val % 2 == 0 } {
            assert!(
                list_remove_data(
                    &mut list,
                    Some(int_equal),
                    val as ListValue,
                ) != 0,
                "list_remove_data(&list, int_equal, val) != 0"
            );
            list_iter_remove(&mut iter);
        }
    }

    list_free(list);
}
